package com.zyl.Ecs.device.web.controller;

import com.zyl.Ecs.common.domain.device.ext.PositionExt;
import com.zyl.Ecs.common.domain.device.vo.PositionVO;
import com.zyl.Ecs.common.response.Result;
import com.zyl.Ecs.common.response.ResultCode;
import com.zyl.Ecs.device.service.PositionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.device.web.controller
 * @ClassName: PositionController
 * @Description TODO (位置控制器)
 * @Author 翟玉龙
 * @Date 2024/12/5-10:18
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/position")
public class PositionController {

    private final PositionService positionService;

    /**
     * 添加点位
     * @param positionVO 位置
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping
    public Result<?> add(
            @RequestBody PositionVO positionVO
    ) {
        positionService.add(PositionExt.toExt(positionVO));
        return Result.success();
    }

    /**
     * 删除点位
     * @param id 点位ID
     * @return {@link Result }<{@link ? }>
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(
            @PathVariable("id") Integer id
    ) {
        positionService.delete(id);
        return Result.success();
    }

    /**
     * 更新点位
     * @param positionVO 点位信息
     * @return {@link Result }<{@link ? }>
     */
    @PutMapping
    public Result<?> update(
            @RequestBody PositionVO positionVO
    ) {
        positionService.update(PositionExt.toExt(positionVO));
        return Result.success();
    }

    /**
     * 获取所有点位
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping
    public Result<?> list() {
        return Result.success(positionService.getList()
                .stream()
                .map(PositionVO::toVO)
                .collect(Collectors.toList()))
                .apply(ResultCode.SUCCESS.getMessage());
    }
}
